package com.jushu.storbox.task.tqueue;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class UploadTaskScheduler <T extends PriorityTask> extends TaskScheduler<T> {
	
	private ExecutorService runningTasks;
	private int initialCapacity = 0;
	
	public UploadTaskScheduler(int initialCapacity) {
		super(initialCapacity);
		this.initialCapacity = initialCapacity;
//		runningTasks = Executors.newFixedThreadPool(initialCapacity);
		runningTasks = new ThreadPoolExecutor(initialCapacity, initialCapacity, 0L,
				TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(1000));
	}

	@Override
	public void stopTaskExecuteService() {
		if(runningTasks != null && !runningTasks.isShutdown()) {
			runningTasks.shutdownNow();
			runningTasks = null;
		}
	}
	
	@Override
	public void startTaskExecuteService() {
		if(runningTasks == null || runningTasks.isShutdown()) {
//			runningTasks = Executors.newFixedThreadPool(initialCapacity);
			runningTasks = new ThreadPoolExecutor(initialCapacity, initialCapacity, 0L,
					TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(1000));
		}
	}

	@Override
	protected void executeTask(T task) {
		if(runningTasks != null && !runningTasks.isShutdown()) {
			runningTasks.submit(task);
		}
	}
}
